/**
 * 
 */
package web;

import java.util.Calendar;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.IPerson;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import session.UserContainer;
import bus.Login;
import dao.IPersonDAO;

/**
 * @spring.bean id="loginForm"
 * @spring.property name="sessionForm" value="true"
 * @spring.property name="commandName" value="login"
 * @spring.property name="validator" ref="loginValidator"
 * @spring.property name="formView" value="login"
 * 
 * @spring.property name="personService" ref="PERSON_SERVICE"
 * 
 * @author ipeel
 *
 */
public class LoginFormController extends SimpleFormController {
	protected final Log logger = LogFactory.getLog(getClass());
	private IPersonDAO personService;
	
	public ModelAndView onSubmit(
			HttpServletRequest request, 
			HttpServletResponse response, 
			Object command, 
			BindException errors) throws Exception {
		Login loginForm = (Login)command;
		IPerson user = getPersonService().getPersonByUsername(loginForm.getUsername());	
		
	    
	    if(user == null) {
	    	errors.rejectValue("username", "error.not-specified", null, "Username wasn't found.");
	    	return showForm(request, response, errors);
	    }
	    
	    if(!user.getPassword().equals(loginForm.getPassword())) {
	    	errors.rejectValue("password", "error.not-specified", null, "Incorrect password.");
	    	return showForm(request, response, errors);
	    }
	    
	    HttpSession session = request.getSession();
	    
	    UserContainer uc = (UserContainer)session.getAttribute(IConstants.Sessoin.USER_CONTAINER);
	    if(uc == null) {
	    	uc = new UserContainer(); 
	    }
	    
	    uc.setUser(user);
	    session.setAttribute(IConstants.Sessoin.USER_CONTAINER, uc);
	    logger.info("User: "+user.getUsername()+" logged in at "+Calendar.getInstance().getTime());
	    
	    if(uc.getPreAuthURI() != null) {
	    	String redirectParams = new String();
	    	
	    	if(uc.getPreAuthParams() != null) {	    		
	    		Iterator i = uc.getPreAuthParams().iterator();
	    		
	    		while(i.hasNext()){
	    			String param = (String)i.next();
	    			redirectParams+=param;
	    			
	    			if(i.hasNext()) {
	    				redirectParams+="&";
	    			}
	    		}
	    	}
	    	
	    	return new ModelAndView(new RedirectView(uc.getPreAuthURI()+"?"+redirectParams));
	    }
	    
	    
        return new ModelAndView(new RedirectView(IConstants.Views.HOMEPAGE_REDIRECT));
	}
	
	protected Object formBackingObject(HttpServletRequest request) throws ServletException {
		return new Login();
	}

	/**
	 * @return Returns the personService.
	 */
	public IPersonDAO getPersonService() {
		return personService;
	}

	/**
	 * @param personService The personService to set.
	 */
	public void setPersonService(IPersonDAO personService) {
		this.personService = personService;
	}
}